package com.ymatou.iapi.anlimember.testcase;

import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.anlimember.enums.MemberTypeEnum;
import com.ymatou.iapi.anlimember.enums.OpenTypeEnum;
import com.ymatou.iapi.anlimember.parameter.CreateMemberReq;
import com.ymatou.iapi.anlimember.service.CreateMemberCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.anlihui.MemberRelation;
import com.ymttest.database.sqlwapper.anlihui.MemberRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_CreateMember {
	
	private static CreateMemberReq createMemberReq;
	private static CreateMemberCall createMemberCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建会员关系");
	}

	@Before
	public void caseUp() {
		createMemberReq = new CreateMemberReq();
		createMemberCall = new CreateMemberCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createMemberCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public static void verify() {
		
		MemberRelationWrapper wrapper = new MemberRelationWrapper();

		MemberRelation actual = wrapper.selectByMemberIdAndParentId(
				createMemberReq.getUserId(), createMemberReq.getInviterId());

		Logger.verifyEquals(createMemberReq.getOrderId(),
				actual.getBuyvip_order_id(), "验证Buyvip_order_id()");
		Logger.verifyEquals((byte) createMemberReq.getMemberTypeEnum()
				.getCode(), actual.getMember_level(), "验证Member_level");
		Logger.verifyEquals((byte) OpenTypeEnum.SELF_BUY.getCode(), actual.getOpen_type(), "验证Open_type");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_001() {
		Logger.start(true, "创建安利会员关系成功");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(3002L);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_002() {
		Logger.start(true, "创建海淘达人会员关系成功");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(3002L);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.HAITAO_DAREN);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_003() {
		Logger.start(true, "邀请人不是会员，创建会员关系失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(2222L);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(1122211L);
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(202, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("只有邀请人是会员才能分享链接邀请购买会员!",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_004() {
		Logger.start(true, "用户已经开通会员");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(3002L);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(1111L);
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(202, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("用户已经开通过会员",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_005() {
		Logger.start(true, "inviterId为null，创建会员成功");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_006() {
		Logger.start(true, "MemberType为null，创建会员失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(null);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(400, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("memberTypeEnum:会员类型不能为空",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_007() {
		Logger.start(true, "orderId为null，创建会员失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(null);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(400, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("orderId:订单号不能为空",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_008() {
		Logger.start(true, "PayTime为null，创建会员失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(null);
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(400, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("payTime:订单支付时间不能为空",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_009() {
		Logger.start(true, "UserId为null，创建会员失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(null);
			createMemberReq.setVipDeadline(calendar.getTime());
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(400, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("userId:买家id不能为空",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_010() {
		Logger.start(true, "VipDeadline为null，创建会员失败");
		try {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(new Date());
			calendar.add(Calendar.YEAR, 3);
			
			createMemberReq.setInviterId(null);
			createMemberReq.setMemberTypeEnum(MemberTypeEnum.ANLI);
			createMemberReq.setOrderId(1111111111111L);
			createMemberReq.setPayTime(new Date());
			createMemberReq.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			createMemberReq.setVipDeadline(null);
			
			createMemberCall.setData(createMemberReq);
			createMemberCall.callService();
			
			Logger.verifyEquals(400, createMemberCall.getCode(), "验证code");
			Logger.verifyEquals("vipDeadline:会员有效期",
					createMemberCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
